package com.project.lk.nurseapp.home;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hujiang.permissiondispatcher.NeedPermission;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.babyvoice.BabyVoiceActivity;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.base.BaseApp;
import com.project.lk.nurseapp.base.UrlContants;
import com.project.lk.nurseapp.bloodglucose.BloodGlucoseActivity;
import com.project.lk.nurseapp.common.view.MyGridView;
import com.project.lk.nurseapp.db.UserDaoBeanDao;
import com.project.lk.nurseapp.doctor.DoctorActivity;
import com.project.lk.nurseapp.doctor.DoctorDetailsActivity;
import com.project.lk.nurseapp.healthrecord.HealthRecordActivity;
import com.project.lk.nurseapp.home.presenter.IUserView;
import com.project.lk.nurseapp.home.presenter.UserPresenter;
import com.project.lk.nurseapp.home.usermsg.UserMsgActivity;
import com.project.lk.nurseapp.inputfiles.InputFilesActivity;
import com.project.lk.nurseapp.login.LoginActivity;
import com.project.lk.nurseapp.login.model.UserBean;
import com.project.lk.nurseapp.msg.AppMsgActivity;
import com.project.lk.nurseapp.msg.AppMsgDetailsActivity;
import com.project.lk.nurseapp.nurse.NurseActivity;
import com.project.lk.nurseapp.remind.RemindActivity;
import com.project.lk.nurseapp.sphy.SphyActivity;
import com.project.lk.nurseapp.thermomter.ThermomterActivity;
import com.project.lk.nurseapp.ticket.TicketActivity;
import com.project.lk.nurseapp.utils.AppManager;
import com.project.lk.nurseapp.utils.GlideCircleTransform;
import com.project.lk.nurseapp.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class MainActivity extends BaseActivity  implements IUserView{


    @BindView(R.id.home_about)
    TextView homeAbout;
    @BindView(R.id.home_icon)
    ImageView homeIcon;
    @BindView(R.id.home_name)
    TextView homeName;
    @BindView(R.id.setting_gv)
    MyGridView settingGv;
    @BindView(R.id.intelligent_gv)
    MyGridView intelligentGv;


    String[] homeSetting =new String[]{
       "护士站",   "系统消息"  ,"提醒"
            ,"医嘱",   "健康档案"  ,"录入档案"
            ,"优惠券"
    };
    String[] homeIntelligent =new String[]{
            "血压计",   "体温计"  ,"血糖仪","胎心仪"
    };
    int[] settinIcon =new int[]{
            R.drawable.hushizhan,   R.drawable.xiaoxi  ,R.drawable.tixing
            ,  R.drawable.yizhu,   R.drawable.jiankang  ,R.drawable.luru
            ,R.drawable.youhuiq
    };
    int[] IntelligentIcon =new int[]{
            R.drawable.xueya,   R.drawable.tiwen  ,R.drawable.xuetang,R.drawable.taixin
    };
    private String login="";
    private AlertDialog alertDialog;
    private UserPresenter presenter;
    private UserBean userMsgbean;

    @Override
    protected void initData() {
        login = getIntent().getStringExtra("login");
        if (TextUtils.equals(login,"login")) {
            presenter = new UserPresenter(this);
        }
        setSettingItem();
        setIntelligentItem();
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        alertDialog = dialogBuilder.setTitle("是否退出登录")
                .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        exit();

                    }
                }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                }).setCancelable(false).create();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (presenter!=null) {
            presenter.loadUserData(UrlContants.USER_INFO_URL);
        }
    }



    //退出登录
    private void exit() {
        UserDaoBeanDao userDaoBeanDao = BaseApp.getInstance().getDaoSession().getUserDaoBeanDao();
        userDaoBeanDao.deleteAll();
        startActivity(new Intent(this, LoginActivity.class));
        AppManager.getAppManager().finishAllActivity();
        alertDialog.dismiss();
    }

    final   int SPHYGMOMANOMETER_POSITION=0;
    final int THERMOMETER_POSITION=1;
    final int BLOOD_GLUCOSE_POSITION=2;
    final int BABY_VOICE_POSITION=3;
    //设置智能看护条目点击事件
    private void setIntelligentItem() {
        List<Map<String,Object>> intelligentList=new ArrayList<>();
        for (int i=0;i<IntelligentIcon.length;i++){
            Map<String,Object> hashMap=new HashMap<>();
            hashMap.put("title",homeIntelligent[i]);
            hashMap.put("icon",IntelligentIcon[i]);
            intelligentList.add(hashMap);
        }
        SimpleAdapter intelligentAdapter=new SimpleAdapter(this,  intelligentList,R.layout.item_grid_layout
                ,new String[]{"title","icon"},new int[]{R.id.item_title,R.id.item_icon});

        intelligentGv.setAdapter(intelligentAdapter);
        intelligentGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case SPHYGMOMANOMETER_POSITION: //血压计
                        startActivity(new Intent(MainActivity.this, SphyActivity.class)
                        .putExtra("login",login));
                        break;
                    case THERMOMETER_POSITION: //体温计
                        startActivity(new Intent(MainActivity.this, ThermomterActivity.class)
                                .putExtra("login",login));
                        break;
                    case BLOOD_GLUCOSE_POSITION: //血糖仪
                        startActivity(new Intent(MainActivity.this, BloodGlucoseActivity.class)
                                .putExtra("login",login));
                        break;
                    case BABY_VOICE_POSITION: //胎心仪
                        startActivity(new Intent(MainActivity.this, BabyVoiceActivity.class)
                                .putExtra("login",login));
                        break;
                }
            }
        });
    }


    final  int NURSE_POSITION=0;
    final int APP_MSG_POSITION=1;
    final  int REMIND_POSITION=2;
    final int DOCTOR_POSITION=3;
    final int HEALTH_RECORDS_POSITION=4;
    final int INPUT_FILES_POSITION=5;
    final int TICKET_POSITION=6;
    //基本设置条目点击事件
    private void setSettingItem() {
        List<Map<String,Object>> settingList=new ArrayList<>();
        for (int i=0;i<settinIcon.length;i++){
            Map<String,Object> hashMap=new HashMap<>();
            hashMap.put("title",homeSetting[i]);
            hashMap.put("icon",settinIcon[i]);
            settingList.add(hashMap);
        }
        SimpleAdapter settingAdapter=new SimpleAdapter(this,  settingList,R.layout.item_grid_layout
                ,new String[]{"title","icon"},new int[]{R.id.item_title,R.id.item_icon});

        settingGv.setAdapter(settingAdapter);
        settingGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case NURSE_POSITION: //护士站
                            AppManager.getAppManager().finishActivity();
                        break;
                    case APP_MSG_POSITION: //系统消息
                        if (TextUtils.equals(login,"login")) {
                            startActivity(new Intent(MainActivity.this, AppMsgActivity.class));
                        }else {
                            ToastUtils.showShortToast("当前离线模式，无法使用此功能");
                        }

                        break;
                    case REMIND_POSITION: //提醒
                            startActivity(new Intent(MainActivity.this, RemindActivity.class));
                        break;
                    case DOCTOR_POSITION: //医嘱
                        if (TextUtils.equals(login,"login")) {
                            startActivity(new Intent(MainActivity.this, DoctorActivity.class));
                        }else {
                            ToastUtils.showShortToast("当前离线模式，无法使用此功能");
                        }

                        break;
                    case HEALTH_RECORDS_POSITION: //健康档案
                        if (TextUtils.equals(login,"login")) {
                            startActivity(new Intent(MainActivity.this, HealthRecordActivity.class));
                        }else {
                            ToastUtils.showShortToast("当前离线模式，无法使用此功能");
                        }

                        break;
                    case INPUT_FILES_POSITION: //录入档案
                        if (TextUtils.equals(login,"login")) {
                            startActivity(new Intent(MainActivity.this, InputFilesActivity.class));
                        }else {
                            ToastUtils.showShortToast("当前离线模式，无法使用此功能");
                        }

                        break;
                    case TICKET_POSITION: //优惠券
                        if (TextUtils.equals(login,"login")) {
                            startActivity(new Intent(MainActivity.this, TicketActivity.class));
                        }else {
                            ToastUtils.showShortToast("当前离线模式，无法使用此功能");
                        }

                        break;
                }
            }
        });
    }


    @Override
    protected void initView() {


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }



    @OnClick({R.id.back, R.id.home_change,R.id.exit_but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:  //返回
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.home_change: //修改个人信息
                if (TextUtils.equals(login,"login")) {
                    if (userMsgbean!=null) {
                        startActivity(new Intent(this, UserMsgActivity.class)
                                .putExtra("user",userMsgbean));
                    }else {
                        ToastUtils.showShortToast("网速较慢，稍后修改");
                    }

                }else {
                ToastUtils.showShortToast("离线模式，无法使用此功能");
                }

                break;
            case R.id.exit_but: //退出当前账号
                if (TextUtils.equals(login,"login")) {
                    alertDialog.show();
                }else {
                    startActivity(new Intent(this, LoginActivity.class));
                    AppManager.getAppManager().finishAllActivity();
                }
                break;
        }
    }

    @Override
    public void returnSuccess() {
        if (presenter!=null) {
            List<UserBean> userBean = presenter.getUserBean();

            if (userBean!=null&&userBean.size()!=0) {
                userMsgbean = userBean.get(0);
                String nickName = userMsgbean.getNickName();
                String headImg = userMsgbean.getHeadImg();
                Glide.with(this).load(headImg)
                        .error(R.drawable.moren)
                        .placeholder(R.drawable.moren)
                        .bitmapTransform(new GlideCircleTransform(this))
                        .into(homeIcon);
                homeName.setText(nickName);
            }
        }
    }

    @Override
    public void fail() {

    }

}
